﻿window.alert = function(message){
    $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html(message)
        .dialog({
            buttons: {OK: function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
        });
};
window.confirm = function(message){
    $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html(message)
        .dialog({
            buttons: {"Xác Nhận": function(){$(this).dialog('close');}, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});
     };
function GoToQuestionPage(deviceId, manufacturerId, modelId, condition, stringToSearch, pageNo, targetDiv) {
        $.ajax({                    
            type: "POST",
            url: "/Question/GetQuestionList",
            dataType: "html",
            data: {
                deviceId: deviceId,
                manufacturerId: manufacturerId,
                modelId: modelId,
                condition: condition,
                stringToSearch: stringToSearch,
                pageNo: pageNo,
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                    document.getElementById(targetDiv).innerHTML = data;
                }
               
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalQuestion").html());
//                            $("#totalQuestion").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };

    function GoToAnswerPage(questionId, condition, pageNo, targetDiv) {
        $.ajax({                    
            type: "POST",
            url: "/Question/GetAnswerList",
            dataType: "html",
            data: {
                questionId: questionId,
                condition: condition,
                pageNo: pageNo,
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                    document.getElementById(targetDiv).innerHTML = data;
                    $("#SelectedAnswerPage").val(pageNo);
                }
               
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };

    function GoToQuestionBookMarksPage(pageNo, targetDiv) {
        $.ajax({                    
            type: "POST",
            url: "/Question/GetQuestionBookmarksList",
            dataType: "html",
            data: {
                pageNo: pageNo,
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                    document.getElementById(targetDiv).innerHTML = data;
                }
               
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };

    function PlusPointForAnAnswer(answerId) {
        
        var pointElementName = 'answerPoint ' + answerId;
        var imageElementName = 'answerPointImage ' + answerId;
        $.ajax({                    
            type: "POST",
            url: "/Question/PlusPointForAnAnswer",
            dataType: "json",
            data: {
                AnswerId: answerId
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                     document.getElementById(pointElementName).innerHTML = data.newPoint;
                    var currentImage = document.getElementById(imageElementName).getAttribute('src');
                    if(currentImage == "/Content/Images/Action-arrow-blue-up.ico") {
                        document.getElementById(imageElementName).setAttribute('src', '/Content/Images/Action-arrow-blue-down.ico');
                        document.getElementById(imageElementName).setAttribute('title', 'Bạn đã cộng điểm cho câu hỏi này. Click tiếp để huỷ');
                    }
                    else {
                     document.getElementById(imageElementName).setAttribute('src', '/Content/Images/Action-arrow-blue-up.ico');
                     document.getElementById(imageElementName).setAttribute('title', 'Click để cộng điểm cho câu hỏi này nếu bạn thấy nó hữu ích. Click tiếp để huỷ');
                    }
                    
                }
               
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    }

    ;
    


    function AcceptAnAnswer(questionId, answerId) {
         $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html("Câu trả lời này đã giúp bạn giải quyết được vấn đề?")
        .dialog({
            buttons: {"Xác Nhận": function() {
                                $.ajax ({
                    type: "POST",
                    url: "/Question/AcceptAnAnswer",
                    dataType: "text",
                    data: {
                        QuestionId: questionId,
                        AnswerId: answerId
                    },
                    success: function (data) {
                        if(data=="true") {
                        ReloadAnswerTab(3, questionId);
                            document.getElementById("isResolvedQuestion").innerHTML = "<img src=\"/Content/Images/Resolved.ico\" width=\40px\" height=\"40px\" title=\"Đã Được Giải Quyết\"/>";
 //                       $('#answerListField').append(data);
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
                        }
                          
                    },
                    error: function (result,error) {
                        alert ( " Can't do because: " + error.responseText );                        
                    },

                    complete: function() {
//                        isPosting = false;
                        $('#answerContent').attr("disable","");
                    }
                });
                $(this).dialog('close');
            }, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});

        };
        

        function DeleteAnAnswer(questionId,answerId) {
            $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html("Bạn có chắc chắn muốn xoá câu trả lời này?")
        .dialog({
            buttons: {"Xác Nhận": function() {
                            var elementName = 'answerFieldSet ' + answerId;
                $.ajax ({
                    type: "POST",
                    url: "/Question/DeleteAnAnswer",
                    dataType: "json",
                    data: {
                        QuestionId: questionId,
                        AnswerId: answerId
                    },
                    success: function (data) {
                        if(data.Success == "true") {
                            if(data.isAcceptedAnswer == "true") {
                                document.getElementById("isResolvedQuestion").innerHTML = "";
                            }
                            var condition = "";
                            if($("#SelectedAnswerTab").val() == 1) {
                                condition = "highScore";
                            }
                            else if($("#SelectedAnswerTab").val() == 2)
                            {
                                condition = "new";
                            }
                            else if($("#SelectedAnswerTab").val() == 3)
                        {
                                condition = "accepted";
                        }
GoToAnswerPage(questionId, condition, $("#SelectedAnswerPage").val(), "answerList");
                            
 //                       $('#answerListField').append(data);
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
                        }
                          
                    },
                    error: function (result,error) {
                        alert ( " Can't do because: " + error.responseText );                        
                    },

                    complete: function() {
//                        isPosting = false;
                        $('#answerContent').attr("disable","");
                    }
                });
                $(this).dialog('close');
            }, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});

        };
    
    function ReloadAnswerTab(index, QuestionId) {
        //Set lai bottomBorder cho cac tab
        for (var i = 1; i <= 3; i++) {
            var divId = "tabAnswerContent-" + i;
            var divElement = document.getElementById(divId);
            var tabId = "tabAnswer" + i;
            var myTab = document.getElementById(tabId);
            if (i != index) {
                divElement.innerHTML = "";
                myTab.style.borderBottom = "1px solid #808080";
            } else {
                divElement.innerHTML = "";
                myTab.style.borderBottom = "solid 1px white";
            }
        }

        var targetDiv = "tabAnswerContent-" + index;
        //$(targetDiv).html("<img src='@Url.Content("~/Content/images/main/loading.gif")' style='margin-left: 455px;'/>");
        document.getElementById(targetDiv).innerHTML = "Đang tải dữ liệu...";

        //set url and data to send
        var url;
        var condition;
        if (index == 1) {
            condition = "highScore";
        }
        else if (index == 2) {
            condition = "new";
        }
        else if (index == 3) {
            condition = "accepted";
        }
        url = "/Question/GetAnswerList?questionId="+QuestionId+"&condition="+condition;
        $.get(url, function (result) {
            document.getElementById(targetDiv).innerHTML = result;
        });
    }


    function GoToGuidePage(deviceId, manufacturerId, modelId, condition, stringToSearch, pageNo, targetDiv) {
        $.ajax({                    
            type: "POST",
            url: "/Guide/GetGuideList",
            dataType: "html",
            data: {
                deviceId: deviceId,
                manufacturerId: manufacturerId,
                modelId: modelId,
                condition: condition,
                stringToSearch: stringToSearch,
                pageNo: pageNo,
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                    document.getElementById(targetDiv).innerHTML = data;
                }
               
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalQuestion").html());
//                            $("#totalQuestion").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };
     function GoToCommentPage(guideId, condition, pageNo, targetDiv) {
        $.ajax({                    
            type: "POST",
            url: "/Guide/GetCommentList",
            dataType: "html",
            data: {
                guideId: guideId,
                condition: condition,
                pageNo: pageNo,
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                    document.getElementById(targetDiv).innerHTML = data;
                    $("#SelectedCommentPage").val(pageNo);
                }
               
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };
    //PlusPointForAnComment
     function PlusPointForAnComment(commentId) {
        
        var pointElementName = 'commentPoint ' + commentId;
         var textName = 'VoteCommentText ' + commentId;
        $.ajax({                    
            type: "POST",
            url: "/Guide/PlusPointForAnComment",
            dataType: "json",
            data: {
                CommentId: commentId
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                     document.getElementById(pointElementName).innerHTML = data.newPoint;
                    var currentText = document.getElementById(textName);
                    if(currentText.innerHTML == "Thích") {
                        document.getElementById(textName).innerHTML = "Bỏ thích";
                    }
                    else {
                     document.getElementById(textName).innerHTML = "Thích";
                    }
                    
                }
               
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };

    

    function ReportAnComment(commentId){
        var textName = 'ReportCommentText ' + commentId;
                    var currentText = document.getElementById(textName);
        if(currentText.innerHTML == "Báo cáo sai phạm") {
                        $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html("<div>Lý do bạn muốn báo cáo sai phạm bình luận này là:<br/><br/><input type=text id=txtCommentReport style='width:400px'/><br/><div id=inputMessage style='color:red; font-size:14px'></div></div>")
        .dialog({
            buttons: {"Xác Nhận": function() {
                var reason = $("#txtCommentReport").val().trim();
                if(reason != "") {
             var imageElementName = 'reportCommentImage ' + commentId;
        $.ajax({
            type: "POST",
            url: "/Guide/ReportAnComment",
            dataType: "json",
            data: {
                CommentId: commentId,
                Reason: reason
            },
            success: function(data) {
                if (data.message) {
                    alert(data.message);
                } else {
                    
                    if(currentText.innerHTML == "Báo cáo sai phạm") {
                        alert("Báo cáo hoàn tất!<br/>Ban quản trị sẽ xử lý thông tin trong thời gian sớm nhất.");
                        document.getElementById(textName).innerHTML = "Huỷ báo cáo sai phạm";
                    }
                    else {
                     document.getElementById(textName).innerHTML = "Báo cáo sai phạm";
                    }
                }

//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalQuestion").html());
//                            $("#totalQuestion").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        }); 
        
                    $(this).dialog('close');
                } else {
                    document.getElementById("inputMessage").innerHTML = "Lý do sai phạm không thể để trống!";    
                }
                
            }, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});
//            var reason = prompt('Lý do bạn báo cáo sai phạm bình luận này là:', '');
//            if(reason.trim() == "") {
//                alert("Báo cáo sai phạm chưa hoàn tất.\nBạn cần nhập lý do sai phạm!");
//                return;
//            }
//        if(reason != null && reason != "") {
//             var imageElementName = 'reportCommentImage ' + commentId;
//        $.ajax({
//            type: "POST",
//            url: "/Guide/ReportAnComment",
//            dataType: "json",
//            data: {
//                CommentId: commentId,
//                Reason: reason
//            },
//            success: function(data) {
//                if (data.message) {
//                    alert(data.message);
//                } else {
//                    
//                    if(currentText.innerHTML == "Báo cáo sai phạm") {
//                        alert("Báo cáo hoàn tất!\nBan quản trị sẽ kiểm tra và xử lý thông tin trong thời gian sớm nhất.");
//                        document.getElementById(textName).innerHTML = "Huỷ báo cáo sai phạm";
//                    }
//                    else {
//                     document.getElementById(textName).innerHTML = "Báo cáo sai phạm";
//                    }
//                }

////                        var firstli = $('.comment-list li').first();
////                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

////                        if(data != "") {
////                            var total = parseInt($("#totalQuestion").html());
////                            $("#totalQuestion").html(total + 1);
////                        }
//            },
//            error: function(result, error) {
//                alert(" Can't do because: " + error.responseText);
//            },

//            complete: function() {
////                        isPosting = false;
//            }
//        }); 
//        }
        } else {
            $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html("Bạn muốn huỷ báo cáo sai phạm bình luận này?")
        .dialog({
            buttons: {"Xác Nhận": function() {
                                $.ajax({
            type: "POST",
            url: "/Guide/ReportAnComment",
            dataType: "json",
            data: {
                CommentId: commentId
            },
            success: function(data) {
                if (data.message) {
                    alert(data.message);
                } else {
                    
                    if(currentText.innerHTML == "Báo cáo sai phạm") {
                        document.getElementById(textName).innerHTML = "Huỷ báo cáo sai phạm";
                    }
                    else {
                     document.getElementById(textName).innerHTML = "Báo cáo sai phạm";
                    }
                }

//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalQuestion").html());
//                            $("#totalQuestion").html(total + 1);
//                        }
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
                $(this).dialog('close');
            }, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});
        }
        
       
    };


    function ReloadCommentTab(index, GuideId) {
        //Set lai bottomBorder cho cac tab
        for (var i = 1; i < 3; i++) {
            var divId = "tabCommentContent-" + i;
            var divElement = document.getElementById(divId);
            var tabId = "tabComment" + i;
            var myTab = document.getElementById(tabId);
            if (i != index) {
                divElement.style.display = "none";
                myTab.style.borderBottom = "1px solid #808080";
            } else {
                divElement.style.display = "block";
                myTab.style.borderBottom = "solid 1px white";
            }
        }

        var targetDiv = "tabCommentContent-" + index;
        //$(targetDiv).html("<img src='@Url.Content("~/Content/images/main/loading.gif")' style='margin-left: 455px;'/>");
        document.getElementById(targetDiv).innerHTML = "Đang tải dữ liệu...";

        //set url and data to send
        var url;
        var condition;
        if (index == 1) {
            condition = "highScore";
        }
        else if (index == 2) {
            condition = "new";
        }
       
        url = "/Guide/GetCommentList?guideId="+GuideId+"&condition="+condition;
        $.get(url, function (result) {
            document.getElementById(targetDiv).innerHTML = result;
        });
    }

     function DeleteAnComment(guideId,commentId) {
         $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html("Bạn có chắc chắn muốn xoá bình luận này?")
        .dialog({
            buttons: {"Xác Nhận": function() {
                            var elementName = 'commentFieldSet ' + commentId;
                $.ajax ({
                    type: "POST",
                    url: "/Guide/DeleteAnComment",
                    dataType: "text",
                    data: {
                        GuideId: guideId,
                        CommentId: commentId
                    },
                    success: function (data) {
                        if(data=="true") {
                            //document.getElementById(elementName).style.display="none";
                            var condition = "";
                            if($("#SelectedCommentTab").val() == 2) {
                                condition = "new";
                            } else {
                                condition = "highScore";
                            }
                            GoToCommentPage(guideId, condition, $("#SelectedCommentPage").val(), "commentList");
 //                       $('#answerListField').append(data);
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

                            var total = parseInt($("#guideComment").html());
                            $("#guideComment").html(total - 1);
                        }
                          
                    },
                    error: function (result,error) {
                        alert ( " Can't do because: " + error.responseText );                        
                    },

                    complete: function() {
//                        isPosting = false;
                        $('#answerContent').attr("disable","");
                    }
                });
                $(this).dialog('close');
            }, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});

        };


        function GoToGuideBookMarksPage(pageNo, targetDiv) {
        $.ajax({                    
            type: "POST",
            url: "/Guide/GetGuideBookmarksList",
            dataType: "html",
            data: {
                pageNo: pageNo,
            },
            success: function(data) {
                if(data.message) {
                    alert(data.message);
                } else {
                    document.getElementById(targetDiv).innerHTML = data;
                }
               
            },
            error: function(result, error) {
                alert(" Can't do because: " + error.responseText);
            },

            complete: function() {
//                        isPosting = false;
            }
        });
    };


    function initLogin() {
    $("img.open-id").click(function () {
        var site = $(this).attr("data-provider");

        if (site == "google")
            value = "https://www.google.com/accounts/o8/id";
        else if (site == "yahoo")
            value = "https://me.yahoo.com/"
        else
            return false;

        $("#loading").html("<img src='/content/images/loading.gif' style='margin-left: 50px;' />");
        $("#openid_identifier").val(value);
        setTimeout(function () { $("#open-form").submit(); }, 300);
    });
}